/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package CAPA_INTERFAZ_DE_USUARIO;

import CAPA_LOGICA_DE_NEGOCIO.LN_LOGICA_NEGOCIO;
import RECURSOS.CLVALIDACION;
import RECURSOS.LOG;
import RECURSOS.CLIENTE;
import RECURSOS.FONDO_FORM;
import javax.swing.JOptionPane;
import RECURSOS.TIPO_CLIENTE;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JTextField;
/**
 *
 * @author User
 */
public class IU_INGRESO_CLIENTES extends javax.swing.JFrame {
    private FONDO_FORM F;
    CLIENTE ob_cliente;
    @Override    
    public Image getIconImage() {
        Image retValue = Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource("RECURSOS/SOVI.PNG"));
        return retValue;
    }
    /**
     * Creates new form INGRESO_CLIENTES
     */
    public IU_INGRESO_CLIENTES() {
        initComponents();
        this.setLocationRelativeTo(null);
        F=new FONDO_FORM(this.getWidth(),this.getHeight(),"fondo.png");
        this.add(F);
        this.pack();   
        
        this.txt_ci.setInputVerifier(new Verificar_CedulaRuc());        
    }

    public CLIENTE getOb_cliente() {
        return ob_cliente;
    }

    public JTextField getTxt_ci() {
        return txt_ci;
    }
      
    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lbl_titulo = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        txt_ci = new javax.swing.JTextField();
        txt_apellidos = new javax.swing.JTextField();
        cbx_tipocliente = new javax.swing.JComboBox();
        lbl_apellidos = new javax.swing.JLabel();
        lbl_nombres = new javax.swing.JLabel();
        txt_nombres = new javax.swing.JTextField();
        txt_direccion = new javax.swing.JTextField();
        lbl_direccion = new javax.swing.JLabel();
        lbl_telefono = new javax.swing.JLabel();
        txt_telefono = new javax.swing.JTextField();
        btn_guardar = new javax.swing.JButton();
        btn_cancelar = new javax.swing.JButton();
        lbl_tipocliente = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("SOVI");
        setIconImage(getIconImage());
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        lbl_titulo.setFont(new java.awt.Font("Cambria", 1, 18)); // NOI18N
        lbl_titulo.setForeground(new java.awt.Color(0, 0, 102));
        lbl_titulo.setText("INGRESO DE CLIENTES"); // NOI18N

        jLabel1.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 0, 102));
        jLabel1.setText("Cédula/RUC:");

        txt_ci.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        txt_ci.setForeground(new java.awt.Color(0, 0, 102));
        txt_ci.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txt_ciKeyTyped(evt);
            }
        });

        txt_apellidos.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        txt_apellidos.setForeground(new java.awt.Color(0, 0, 102));
        txt_apellidos.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txt_apellidosKeyTyped(evt);
            }
        });

        cbx_tipocliente.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        cbx_tipocliente.setForeground(new java.awt.Color(0, 0, 102));

        lbl_apellidos.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        lbl_apellidos.setForeground(new java.awt.Color(0, 0, 102));
        lbl_apellidos.setText("Apellidos:");

        lbl_nombres.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        lbl_nombres.setForeground(new java.awt.Color(0, 0, 102));
        lbl_nombres.setText("Nombres:");

        txt_nombres.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        txt_nombres.setForeground(new java.awt.Color(0, 0, 102));
        txt_nombres.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txt_nombresKeyTyped(evt);
            }
        });

        txt_direccion.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        txt_direccion.setForeground(new java.awt.Color(0, 0, 102));

        lbl_direccion.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        lbl_direccion.setForeground(new java.awt.Color(0, 0, 102));
        lbl_direccion.setText("Dirección:");

        lbl_telefono.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        lbl_telefono.setForeground(new java.awt.Color(0, 0, 102));
        lbl_telefono.setText("Teléfono:");

        txt_telefono.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        txt_telefono.setForeground(new java.awt.Color(0, 0, 102));
        txt_telefono.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txt_telefonoKeyTyped(evt);
            }
        });

        btn_guardar.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        btn_guardar.setForeground(new java.awt.Color(0, 0, 102));
        btn_guardar.setText("GUARDAR");
        btn_guardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_guardarActionPerformed(evt);
            }
        });

        btn_cancelar.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        btn_cancelar.setForeground(new java.awt.Color(0, 0, 102));
        btn_cancelar.setText("CANCELAR");
        btn_cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_cancelarActionPerformed(evt);
            }
        });

        lbl_tipocliente.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        lbl_tipocliente.setForeground(new java.awt.Color(0, 0, 102));
        lbl_tipocliente.setText("Tipo Cliente");

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/RECURSOS/logosovi.png"))); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(76, 76, 76)
                        .add(jLabel3)
                        .add(35, 35, 35)
                        .add(lbl_titulo))
                    .add(layout.createSequentialGroup()
                        .add(43, 43, 43)
                        .add(lbl_tipocliente)
                        .add(35, 35, 35)
                        .add(cbx_tipocliente, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 196, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(43, 43, 43)
                        .add(jLabel1)
                        .add(29, 29, 29)
                        .add(txt_ci, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 196, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(43, 43, 43)
                        .add(lbl_apellidos)
                        .add(47, 47, 47)
                        .add(txt_apellidos, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 330, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(43, 43, 43)
                        .add(lbl_nombres)
                        .add(48, 48, 48)
                        .add(txt_nombres, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 330, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(43, 43, 43)
                        .add(lbl_direccion)
                        .add(47, 47, 47)
                        .add(txt_direccion, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 330, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(43, 43, 43)
                        .add(lbl_telefono)
                        .add(52, 52, 52)
                        .add(txt_telefono, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 196, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(192, 192, 192)
                        .add(btn_guardar)
                        .add(62, 62, 62)
                        .add(btn_cancelar)))
                .addContainerGap(67, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(43, 43, 43)
                        .add(lbl_titulo))
                    .add(layout.createSequentialGroup()
                        .add(20, 20, 20)
                        .add(jLabel3)))
                .add(35, 35, 35)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(3, 3, 3)
                        .add(lbl_tipocliente))
                    .add(cbx_tipocliente, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(20, 20, 20)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(3, 3, 3)
                        .add(jLabel1))
                    .add(txt_ci, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(20, 20, 20)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(6, 6, 6)
                        .add(lbl_apellidos))
                    .add(txt_apellidos, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(20, 20, 20)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(3, 3, 3)
                        .add(lbl_nombres))
                    .add(txt_nombres, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(17, 17, 17)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(lbl_direccion)
                    .add(txt_direccion, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(11, 11, 11)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(3, 3, 3)
                        .add(lbl_telefono))
                    .add(txt_telefono, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(38, 38, 38)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(btn_guardar)
                    .add(btn_cancelar))
                .addContainerGap(24, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-564)/2, (screenSize.height-487)/2, 564, 487);
    }// </editor-fold>//GEN-END:initComponents
    
    public void cargar_combo_tipo_cliente()
    {
        TIPO_CLIENTE obj_tcliente=new TIPO_CLIENTE();
        LN_LOGICA_NEGOCIO obj_ln=new LN_LOGICA_NEGOCIO();
        try
        {
            this.cbx_tipocliente.setModel(obj_ln.consultar_combo(obj_tcliente));
        }
        catch(NullPointerException e)
        {
            LOG l=new LOG();
             JOptionPane.showMessageDialog(null,obj_ln.error_usuario(obj_tcliente.getError()),"ERROR",JOptionPane.ERROR_MESSAGE);
             String log="Tipo: "+obj_tcliente.getError().getTipo_error()+". Mensaje: "+obj_tcliente.getError().getDescripcion_error();
             l.escribir(log);                          
             this.dispose();
             
             /*
            LOG l=new LOG();
            JOptionPane.showMessageDialog(null,"OCURRIO UN ERROR DE TIPO "+obj_tcliente.getError().getDescripcion_error(),"ERROR",JOptionPane.ERROR_MESSAGE);
            String log = "Tipo: "+obj_tcliente.getError().getTipo_error()+". Mensaje: "+obj_tcliente.getError().getDescripcion_error();
            l.escribir(log);
            */
        }
    }
    
    
    
    private void btn_guardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_guardarActionPerformed
                                
        if(txt_ci.getText().isEmpty() || txt_apellidos.getText().isEmpty()
            ||txt_nombres.getText().isEmpty() ||txt_direccion.getText().isEmpty())
        {
            JOptionPane.showMessageDialog(null, "Llene los campos obligatorios, solo el telefono puede estar vacio", "ERROR",JOptionPane.ERROR_MESSAGE);
        }                               
        else        
        {            
            if((this.txt_telefono.getText().length()<7 || this.txt_telefono.getText().length()>10) && !txt_telefono.getText().isEmpty() )
            {
                JOptionPane.showMessageDialog(null, "Ingrese un numero de telefono valido", "ERROR",JOptionPane.ERROR_MESSAGE);
            }
            else
            {
                    TIPO_CLIENTE ob_tipo_cliente=new TIPO_CLIENTE(String.valueOf(this.cbx_tipocliente.getSelectedItem()));
                    ob_cliente=new CLIENTE(txt_ci.getText(),txt_apellidos.getText(),txt_nombres.getText(),txt_direccion.getText(),txt_telefono.getText());
                    LN_LOGICA_NEGOCIO obj_ln = new LN_LOGICA_NEGOCIO();
                    ob_cliente.setTipo_cli(ob_tipo_cliente);
                    obj_ln.guardar(ob_cliente, ob_tipo_cliente);
                    
                    if(ob_cliente.getError().getTipo_error()==null)
                    {
                        JOptionPane.showMessageDialog(null,"Ingreso Existoso!","Cliente Guardado",JOptionPane.INFORMATION_MESSAGE);            
                        this.dispose();
                    }
                    else
                    {
                        LOG l=new LOG();                
                        String log="Tipo: "+ob_cliente.getError().getTipo_error()+". Mensaje: "+ob_cliente.getError().getDescripcion_error();                                        
                        l.escribir(log);                                                
                    }         
            }
        }
    }//GEN-LAST:event_btn_guardarActionPerformed

    private void btn_cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_cancelarActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btn_cancelarActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
        cargar_combo_tipo_cliente();        
    }//GEN-LAST:event_formWindowOpened

    private void txt_ciKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_ciKeyTyped
        // TODO add your handling code here:
        char car = evt.getKeyChar();
        if((car<'0' || car>'9')) evt.consume();
    }//GEN-LAST:event_txt_ciKeyTyped

    private void txt_telefonoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_telefonoKeyTyped
        char car = evt.getKeyChar();
        if((car<'0' || car>'9')) evt.consume();
    }//GEN-LAST:event_txt_telefonoKeyTyped

    private void txt_apellidosKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_apellidosKeyTyped
        char car = evt.getKeyChar();
        if(!Character.isAlphabetic(car) && !Character.isSpaceChar(car)) evt.consume();        
    }//GEN-LAST:event_txt_apellidosKeyTyped

    private void txt_nombresKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_nombresKeyTyped
        char car = evt.getKeyChar();
        if(!Character.isAlphabetic(car) && !Character.isSpaceChar(car)) evt.consume();

    }//GEN-LAST:event_txt_nombresKeyTyped

    public class Verificar_CedulaRuc extends InputVerifier {
        public boolean verify(JComponent editor) {
            if (editor instanceof JTextField)
            {                
                String clave = ((JTextField)editor).getText();
                if(clave.length()== 10 || clave.length()== 13){                                    
                    return true;
                }
                else
                {
                    JOptionPane.showMessageDialog(null, "La cedula debe tener 10 numeros o el ruc 13 numeros","CI/RUC no Valido",JOptionPane.ERROR_MESSAGE);                   
                    return false;
                }
            }
            return true;
        }
    }
    
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(IU_INGRESO_CLIENTES.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(IU_INGRESO_CLIENTES.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(IU_INGRESO_CLIENTES.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(IU_INGRESO_CLIENTES.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new IU_INGRESO_CLIENTES().setVisible(true);
                
            }
            
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_cancelar;
    private javax.swing.JButton btn_guardar;
    private javax.swing.JComboBox cbx_tipocliente;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel lbl_apellidos;
    private javax.swing.JLabel lbl_direccion;
    private javax.swing.JLabel lbl_nombres;
    private javax.swing.JLabel lbl_telefono;
    private javax.swing.JLabel lbl_tipocliente;
    private javax.swing.JLabel lbl_titulo;
    private javax.swing.JTextField txt_apellidos;
    private javax.swing.JTextField txt_ci;
    private javax.swing.JTextField txt_direccion;
    private javax.swing.JTextField txt_nombres;
    private javax.swing.JTextField txt_telefono;
    // End of variables declaration//GEN-END:variables
}
